package org.jeecg.modules.teacher.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.SysUserTit;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.SysUserCacheInfo;
import org.jeecg.modules.teacher.entity.AstuTeacher;
import org.jeecg.modules.teacher.service.IAstuTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 教师信息表
 * @Author: jeecg-boot
 * @Date:   2020-11-05
 * @Version: V1.0
 */
@Api(tags="教师信息表")
@RestController
@RequestMapping("/teacher/astuTeacher")
@Slf4j
public class AstuTeacherController extends JeecgController<AstuTeacher, IAstuTeacherService> {
	@Autowired
	private IAstuTeacherService astuTeacherService;
	@Autowired
	ISysBaseAPI iSysBaseAPI;

	/**
	 * 分页列表查询
	 *
	 * @param astuTeacher
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "教师信息表-分页列表查询")
	@ApiOperation(value="教师信息表-分页列表查询", notes="教师信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AstuTeacher astuTeacher,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AstuTeacher> queryWrapper = QueryGenerator.initQueryWrapper(astuTeacher, req.getParameterMap());
		Page<AstuTeacher> page = new Page<AstuTeacher>(pageNo, pageSize);
		IPage<AstuTeacher> pageList = astuTeacherService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param astuTeacher
	 * @return
	 */
	@AutoLog(value = "教师信息表-添加")
	@ApiOperation(value="教师信息表-添加", notes="教师信息表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AstuTeacher astuTeacher) {
		astuTeacherService.save(astuTeacher);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param teacher
	 * @return
	 */
	@AutoLog(value = "教师信息表-编辑")
	@ApiOperation(value="教师信息表-编辑", notes="教师信息表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AstuTeacher teacher) {
		System.out.println("前端收到的数据信息=====》"+teacher.toString());
		//修改教师信息  不饿修改工号
		astuTeacherService.updateTeacherInfo(teacher);
		//修改用户信息
		if(teacher.getUserId().toString().equals("")){
			return Result.error(500, "修改失败");
		}else {
			SysUserTit user = astuTeacherService.selectUer(teacher.getUserId().toString());
			System.out.println("user======>"+user);
			user.setRealname(teacher.getTeacherName());
			if(teacher.getTeacherSex().equals("男")){
				user.setSex(1);
			}else {
				user.setSex(2);
			};
			astuTeacherService.updateUserInfo(user);
		}
		return Result.OK("成功！");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "教师信息表-通过id删除")
	@ApiOperation(value="教师信息表-通过id删除", notes="教师信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		在删除之前先获得teacher的userid
		AstuTeacher teacher = astuTeacherService.getById(id);
		String userId = teacher.getUserId();
//		删除教师信息
		astuTeacherService.removeById(id);
//		删除用户信息
		int info = astuTeacherService.deleteUserById(userId);
		System.out.println("info======>"+info);
		if (info>0){
			return Result.OK("删除成功!");
		}else {
			return Result.OK("删除失败!");
		}
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "教师信息表-批量删除")
	@ApiOperation(value="教师信息表-批量删除", notes="教师信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		//删除用户
		List<String> list = Arrays.asList(ids.split(","));
		for (String teacherid : list) {
			// 这里的id是teacher 的id ，而不是userid
			//得到teacher的userid
			String userId = astuTeacherService.getById(teacherid).getUserId();
			astuTeacherService.deleteUserById(userId);
		}
		//删除教师
		this.astuTeacherService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "教师信息表-通过id查询")
	@ApiOperation(value="教师信息表-通过id查询", notes="教师信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AstuTeacher astuTeacher = astuTeacherService.getById(id);
		if(astuTeacher==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(astuTeacher);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param astuTeacher
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AstuTeacher astuTeacher) {
        return super.exportXls(request, astuTeacher, AstuTeacher.class, "教师信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AstuTeacher.class);
    }

}
